package com.yofla.coverflow.view 
{
	import com.yofla.coverflow.ApplicationFacade;
	import com.yofla.coverflow.model.AppProxy;
	import com.yofla.coverflow.model.ConfigProxy;
	import com.yofla.coverflow.model.FeedProxy;
	import fl.containers.UILoader;
	import flash.display.Sprite;
	import flash.events.GestureEvent;
	import flash.events.TransformGestureEvent;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	
	/**
	 * ...
	 * @author Metal Slime
	 */
	public class BigImgMediator extends Mediator 
	{
		public static const NAME:String = "BigImgMediator";
		private var _imgbox:Sprite;
		private var _loader:UILoader;
		private var _appProxy:AppProxy;
		private var _configProxy:ConfigProxy;
		private var _feedProxy:FeedProxy;
		
		private var _current:int = 0;
		private var _orgx:Number;
		private var _orgy:Number;
		
		public function BigImgMediator(view:Object) 
		{
			super(NAME, view);
			_imgbox = view as Sprite;	 
			_loader = _imgbox.getChildByName("loader") as UILoader;	 
		}
		
		public function init():void {
			
		}
		
		override public function listNotificationInterests():Array 
		{	
			return [
				ApplicationFacade.XML_LOADED,
				ApplicationFacade.NEW_CURRENT,
			];
		}
		
		override public function handleNotification(notification:INotification):void 
		{
			switch(notification.getName()) {
				case ApplicationFacade.XML_LOADED:
					_appProxy = facade.retrieveProxy( AppProxy.NAME ) as AppProxy;
					_configProxy  = facade.retrieveProxy( ConfigProxy.NAME ) as ConfigProxy;
					_feedProxy    = facade.retrieveProxy( FeedProxy.NAME ) as FeedProxy; 
					
					var w:Number = _configProxy.bigImgWidth;
					var h:Number = _configProxy.bigImgHeight;
					var g:Graphics = _imgbox.graphics;
					g.beginFill(0, 0);
					g.drawRect( -w / 2, -h / 2, w, h);
					g.endFill();
					
					_loader.width = w;
					_loader.height = h;
					_loader.x = -w / 2;
					_loader.y = -h / 2;
					
					_current = _configProxy.current;
					setImg(_configProxy.current);
					
					_orgx = _imgbox.x = _appProxy.appWidth / 2;
					_orgy = _imgbox.y = h / 2 + _configProxy.bigImgTop;
					
					_imgbox.addEventListener(TransformGestureEvent.GESTURE_ZOOM, onZoom);
					_imgbox.addEventListener(TransformGestureEvent.GESTURE_ROTATE, onRotate);
					
					break;
				case ApplicationFacade.NEW_CURRENT:
					var id:uint = notification.getBody() as uint;
					setImg(id);
			}
		}
		
		private function onRotate(e:TransformGestureEvent):void 
		{
			_imgbox.rotation += e.rotation;
		}
		
		private function onZoom(e:TransformGestureEvent):void 
		{
			var psx:Number = _imgbox.scaleX;
			var psy:Number = _imgbox.scaleY;
			psx *= e.scaleX;
			psy *= e.scaleY;
			if (psx > 2 || psy > 2) {
				_imgbox.scaleX = _imgbox.scaleY = 2;
			}
			else if (psx < 0.8 || psy < 0.8) {
				_imgbox.scaleX = _imgbox.scaleY = 0.8;
			}
			else {
				_imgbox.scaleX *= e.scaleX;
				_imgbox.scaleY *= e.scaleX;	
			}
		}
		
		private function setImg(id:uint):void {
			if (id != _current) {
				//_imgbox.x = _orgx;
				//_imgbox.y = _orgy;
				_imgbox.scaleX = _imgbox.scaleY = 1;
				_imgbox.rotation = 0;
			}
			_current = id;	 
			_loader.unload();
			var imageUrlPrefix : String = _configProxy.xmldata.bigImageURLprefix;
			_loader.source = imageUrlPrefix + _feedProxy.xmldata..children()[_current].bigImg;
		}
		
		public function get imgage():Sprite 
		{
			return _imgbox;
		}
		
		public function get loader():UILoader 
		{
			return _loader;
		}
		
	}

}